package com.yanqu.road.server.manger.activity.yingxin;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.yingxin.config.HotPotCollectConfig;
import com.yanqu.road.entity.activity.yingxin.config.YingXinConfig;
import com.yanqu.road.entity.activity.yingxin.config.YingXinMultiConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.YingXinBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.yingxin.YingXinModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 迎新夜宴
 */
public class YingXinMgr extends TempMgr {

    private static YingXinConfig config;

    public static void reloadActivityData() {
        getLogger().info("reload YingXinMgr start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.YingXinActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            config = null;
            getLogger().info("no YingXin activity in show time.");
            return;
        }
        ActivityInfo tmpActivityInfo = openActivityInfoList.get(0);

        YingXinConfig tmpConfig = new YingXinConfig();
        tmpConfig.setActivityInfo(tmpActivityInfo);

        tmpConfig.setFloorMap(YingXinBussiness.getHotPotFloorConfigMap(tmpActivityInfo.getActivityId()));

        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityInfo.getActivityId());

        tmpConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.HOTPOT_SCORE_ITEM_ID.getName()).getIntValue());
        tmpConfig.setEnergyItemId(activityConfigMap.get(eActivityConfigType.HOTPOT_POWER_RECOVER_ITEM_ID.getName()).getIntValue());
        tmpConfig.setBoxItemId(activityConfigMap.get(eActivityConfigType.HOTPOT_BOX_ITEM_ID.getName()).getIntValue());

        tmpConfig.setPassBoxList(activityConfigMap.get(eActivityConfigType.HOTPOT_FINAL_REWARD.getName()).getIntListValue("\\|"));
        tmpConfig.setMaxEnergy(activityConfigMap.get(eActivityConfigType.HOTPOT_POWER_MAX_NUM.getName()).getIntValue());

        List<Integer> recParamList = activityConfigMap.get(eActivityConfigType.HOTPOT_POWER_RECOVER_TIME.getName()).getIntListValue(";");
        tmpConfig.setRecoveryTime(recParamList.get(0));
        tmpConfig.setRecoveryCount(recParamList.get(1));

        List<HotPotCollectConfig> configList = new ArrayList<>();
        String potParam = activityConfigMap.get(eActivityConfigType.HOTPOT_POT_PARAM.getName()).getValue();
        List<String> potParamList = StringUtils.stringToStringList(potParam, "\\|");
        for (String param : potParamList) {
            HotPotCollectConfig tmp = new HotPotCollectConfig();
            List<String> list = StringUtils.stringToStringList(param, ";");
            tmp.setIndex(configList.size());
            tmp.setWeight(Integer.valueOf(list.get(0)));
            tmp.setNeedBallCount(Integer.valueOf(list.get(1)));
            tmp.setOneBallScore(Integer.valueOf(list.get(2)));
            tmp.setFullReward(list.get(3));
            configList.add(tmp);
        }
        tmpConfig.setCollectList(configList);

        List<Integer> ratioList = activityConfigMap.get(eActivityConfigType.HOTPOT_FOOD_PARAM.getName()).getIntListValue(";");
        tmpConfig.setSameColorRatioA(ratioList.get(0));
        tmpConfig.setSameColorRatioB(ratioList.get(1));
        tmpConfig.setSameColorRatioC(ratioList.get(2));
        tmpConfig.setBoxRatio(ratioList.get(3));

        Map<Integer, YingXinMultiConfig> multiConfigMap = new ConcurrentHashMap<>();
        String multiParam = activityConfigMap.get(eActivityConfigType.HOTPOT_MULTIPLE_MODE_PARAM.getName()).getValue();
        List<String> multiList = StringUtils.stringToStringList(multiParam, "\\|");
        for (String multi : multiList) {
            List<Integer> list = StringUtils.stringToIntegerList(multi, ";");
            YingXinMultiConfig multiConfig = new YingXinMultiConfig();
            multiConfig.setMulti(list.get(0));
            multiConfig.setUnlockScore(list.get(1));
            multiConfig.setUnlockVipLv(list.get(2));
            multiConfigMap.put(multiConfig.getMulti(), multiConfig);
        }
        tmpConfig.setMultiMap(multiConfigMap);

        config = tmpConfig;
        getLogger().info("reload YingXinMgr finish! activityId = " + config.getActivityInfo().getActivityId());

        // 在线玩家操作
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            YingXinModule module = player.getModule(YingXinModule.class);
            module.checkUserData();
            module.syncConfig();
            module.syncData();
        }
    }

    public static ActivityInfo getActivityInfo() {
        return config == null ? null : config.getActivityInfo();
    }

    public static YingXinConfig getConfig() {
        return config;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
